#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年5月14日

@author: chenyongbing
'''
import sys,os
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

from employee_rssi import myEmployeeRssi
from base.retail_datas import myRetailDatas
from base.zhimind_datas import myZhimindDatas
from base.time_method import myTimeMethod
from datatuning_datas import myDataTuningDatas
import logging
class AnalyzeEmployeeRssi():
    def __init__(self):
        pass
    
    def save_employee_rssi_to_tab(self,comp_group=None,startTime=None,endTime=None):
        if comp_group == None:
            comp_groups = myZhimindDatas.get_groups_from_employee()
        else:
            comp_groups = [comp_group]
        days = myTimeMethod.get_slist_between_st_et(startTime, endTime)
        for comp_group in comp_groups:
            employees = myZhimindDatas.get_employees( mtype=1, group=comp_group)
            shop_groups = myRetailDatas.get_groups_by_comp(comp_group =comp_group)
            logging.warn('Found Employees %s groups %s in comp %s'%(len(employees),len(shop_groups),comp_group))
            for shop_group in shop_groups:
                datas = []
                for day in days:
                    
                    devDict = myEmployeeRssi.stat_rssi_data_by_device(group=str(shop_group), st=day, et=day,employees=employees)
                    for device , rssiDict in devDict.iteritems():
                        for rssi,count in rssiDict.iteritems():
                            datas.append([day,device,rssi,count,count])
                
                myDataTuningDatas.insert_employee_rssi( datas = datas)
                datas = []
    def save_employee_rssi_with_mac_to_tab(self,comp_group=None,shop_group=None,startTime=None,endTime=None):
        if comp_group == None and shop_group==None:
            comp_groups = myZhimindDatas.get_groups_from_employee(mtype=1)
        elif comp_group==None:
            comp_group = myRetailDatas.get_comp_group(shop_group = shop_group)
            comp_groups = [comp_group]
        else:
            comp_groups = [comp_group]
        days = myTimeMethod.get_slist_between_st_et(startTime, endTime)
        for comp_group in comp_groups:
            employees = myZhimindDatas.get_employees( mtype=1, group=comp_group)
            
            
            if shop_group==None:
                shop_groups = myRetailDatas.get_groups_by_comp(comp_group =comp_group)
            else:
                shop_groups = [shop_group]
            logging.info('Found Employees %s groups %s in comp %s'%(len(employees),len(shop_groups),comp_group))
            for shop_group in shop_groups:
                datas = []
                for day in days:
#                     sysEmployees = myZhimindDatas.get_employees(mtype=0,group=shop_group,day=day)
#                     logging.info('Found System Employees %s  in group %s'%(len(sysEmployees),shop_groups))
                    devDict = myEmployeeRssi.stat_rssi_data_by_device_mac(group=str(shop_group), st=day, et=day,employees=employees)
                    for device , macDict in devDict.iteritems():
                        for mac ,rssiDict in macDict.iteritems():
                            for rssi,count in rssiDict.iteritems():
                                datas.append([day,device,mac,rssi,count,1,count])
                
                myDataTuningDatas.new_insert_employee_rssi( datas = datas)
                datas = []
            shop_group = None    
    def save_sys_employee_rssi_with_mac_to_tab(self,comp_group=None,shop_group=None,startTime=None,endTime=None):
        if  shop_group!=None:
            shop_groups = [shop_group]
        elif comp_group!=None:
            shop_groups = myRetailDatas.get_groups_by_comp(comp_group =comp_group)
        else:
            shop_groups = myZhimindDatas.get_groups_from_employee(mtype=0)
        
        
        days = myTimeMethod.get_slist_between_st_et(startTime, endTime)

        for shop_group in shop_groups:
            datas = []
            for day in days:
                logging.info('Start To Run Group %s Day %s'%(shop_group,day))
                sysEmployees = myZhimindDatas.get_employees(mtype=0,group=shop_group,day=day)
                logging.info('Found System Employees %s  in group %s'%(len(sysEmployees),shop_groups))
                devDict = myEmployeeRssi.stat_rssi_data_by_device_mac(group=str(shop_group), st=day, et=day,employees=sysEmployees)
                for device , macDict in devDict.iteritems():
                    for mac ,rssiDict in macDict.iteritems():
                        for rssi,count in rssiDict.iteritems():
                            datas.append([day,device,mac,rssi,count,0,count])
            
            myDataTuningDatas.new_insert_employee_rssi( datas = datas)
            datas = []
      
if __name__=='__main__':
    logging.basicConfig(level=logging.DEBUG)
    
    
    import argparse,re,datetime,time
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--startTime',metavar=u"starttime",default=None)
    parser.add_argument('--endTime',metavar=u"endtime",default=None)
    parser.add_argument('--comp_group',metavar=u"company groups",default=None)
    parser.add_argument('--comp_id',metavar=u"company id",default=None)
    parser.add_argument('--shop_group',metavar=u"shop group",default=None)
    args = parser.parse_args()
    
    startTime  = args.startTime
    endTime = args.endTime
    
    comp_group = args.comp_group
    shop_group = args.shop_group
    
    if startTime == None or endTime == None:
        startTime = (datetime.date.today()-datetime.timedelta(1)).strftime('%Y-%m-%d')
        endTime = (datetime.date.today()-datetime.timedelta(1)).strftime('%Y-%m-%d')
    
    
    myAnalyzeEmployeeRssi = AnalyzeEmployeeRssi()
    logging.info('Start To Run Real Employee RSSI.')
    myAnalyzeEmployeeRssi.save_employee_rssi_with_mac_to_tab(comp_group=comp_group,shop_group=shop_group,startTime = startTime,endTime = endTime)
    logging.info('Start To System  Employee RSSI.')
    myAnalyzeEmployeeRssi.save_sys_employee_rssi_with_mac_to_tab(comp_group=comp_group, shop_group=shop_group, startTime=startTime, endTime=endTime)
    
    
    